﻿using System.Text.RegularExpressions;

namespace RegexpMatching.Matchers
{
  /// <summary>
  /// Represents class with static methods for matching email adresses
  /// </summary>
  public class EMailMatcher
  {

    // (?!\.)(?!^\d+\@.*)(((?!\.\.)\.?)|([a-zA-Z0-9]))* - parses login
    //    (?!\.) - doesn't start with dot
    //    (?!^\d+\@.*) - login contains not only numbers
    //    (((?!\.\.)\.?)|([a-zA-Z0-9-])) - login body
    //        ((?!\.\.)\.?) - dot, not more one in succession
    // \@ - at
    // (([a-zA-Z0-9]{3,20}\.){1,2})(([a-zA-Z]){2,8})(?<!domain)  - parses domain
    //    (([a-zA-Z0-9]{3,20}\.){1,2}) - 1 or 2 domain name's parts, ends with dot. (from 3 to 20 symbols)
    //    (([a-zA-Z]){2,8})(?<!domain) - upper somain name. from 2 to 8 symbols, is not "domain"

    /// <summary>
    ///  Regexp for e-mail matching
    /// </summary>
    private const string EmailRegexp = @"^(?!\.)(?!^\d+\@.*)(((?!\.\.)\.?)|([a-zA-Z0-9_-]))*\@(([a-zA-Z0-9]{1,20}\.){1,2})(([a-zA-Z]){2,8})(?<!domain)$";

    /// <summary>
    /// Default constructor (hided)
    /// </summary>
    private EMailMatcher()
    {
    }

    /// <summary>
    ///  Tests if input string matches e-mail
    /// </summary>
    /// <param name="input">string to be tested</param>
    /// <returns>true if string represents email, false otherwise</returns>
    public static bool IsMatch(string input)
    {
      input = input.Trim();
      return Regex.IsMatch(input, EmailRegexp, RegexOptions.IgnoreCase);
    }
  }
}
